import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/model/gate_model.dart';
import 'package:management/model/open_gate_record_model.dart';
import 'package:management/utils/convert.dart';
import 'package:management/utils/log_utils.dart';

///MscChannel 通道处理的数据模型 语音识别

class MscChannelModel {
  final String? channelName;
  final Map? message;

  MscChannelModel({this.channelName = "MscChannel", @required this.message});

  Map toMap() {
    var map = {
      "channelName": channelName,
      "message": message,
    };
    return map;
  }
}

///语音识别结果数据模型
class RecognizerResultModel {
  RecognizerResultModel(this.recognizerResult);

  ///语音识别文字结果
  ///e.g. 【结果】开启闸门【置信度】61
  String recognizerResult;

  Map<String, Object> toMap() {
    Map<String, Object> map = {
      "recognizerResult": recognizerResult,
    };
    return map;
  }

  RecognizerResultModel.fromMap(Map map)
      : recognizerResult = map["recognizerResult"];

  /// 处理语音识别转换的文字结果，控制闸门
  static Future<Map<String, Object?>?> dealWithRecognizer(String recognizerResult,
      {String? passerby}) async {
    MyLog.i("dealWithRecognizer:}");
    Map<String, Object?>? openResult = {};
    List split = recognizerResult.split("【结果】")[1].split("【置信度】");
    List a = [];
    if (split[0].contains("开启")) {
      a = split[0].split("开启"); //
    } else if (split[0].contains("打开")) {
      a = split[0].split("打开");
    }
    MyLog.w("a: $a?");
    String gateName = a[1];
    //根据从语音结果分析出的闸门名字 进行数据库查询
    GateModel? gateModel = await GateProvider.instance.queryByName(gateName);
    final BasicMessageChannel mscChannel =
        BasicMessageChannel("MscChannel", StandardMessageCodec());
    if (gateModel != null) {
      if (gateModel.departmentId == "0") {//开启车牌识别相机
        gateModel.openLPRGate(gateModel.deviceId);
        return null;
      }
      gateModel.openFaceGate(
          gateRecordModel: GateRecordModel(
              controlType: 1,
              time: Convert.currentTime(),
              gateMac: gateModel.mac,
              operator: SpUtil.getString("userName"),
              passerby: TextUtil.isEmpty(passerby) ? "空" : passerby,
              gateName: gateName),
          openGateResultCallback: (result) {
            MyLog.w("开闸结果：${result.toMap()}");
            openResult = result.toMap();

            mscChannel.send(
              MscChannelModel(message: {"cmd": "speak", "data": openResult})
                  .toMap(),
            );
          });
    } else {
      Fluttertoast.showToast(msg: "没有找到闸门");
      openResult =
          OpenGateResultModel(openGate: false, failMessage: "没有找到闸门")
              .toMap();
      mscChannel.send(
        MscChannelModel(message: {"cmd": "speak", "data": openResult}),
      );
    }
    return openResult;
  }
}

///开闸结果
class OpenGateResultModel {
  OpenGateResultModel({
    this.channelName = "MscChannel",
    @required this.openGate,
    @required this.failMessage,
  });

  final String channelName;

  /// true 开闸成功
  bool? openGate;

  ///开闸失败原因
  String? failMessage;

  Map<String, Object?> toMap() {
    Map<String, Object?> map = {
      "openGateResult": openGate,
      "failMessage": failMessage,
    };
    return map;
  }
}
